package com.atguigu.champter5.Flink05.sink;

import com.atguigu.beans.WaterSensor;
import com.atguigu.source.RandomWaterSenson;
import org.apache.flink.api.common.serialization.Encoder;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.util.concurrent.TimeUnit;

public class Flink01_Sink_File {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<String> map = env
                .addSource(new RandomWaterSenson())
                .map(WaterSensor::toString);

        final StreamingFileSink<String> sink = StreamingFileSink
                .forRowFormat(
                        new Path("datas/output"),
                        new SimpleStringEncoder<String>("UTF-8")).withRollingPolicy(
                        DefaultRollingPolicy.builder()
                                .withRolloverInterval(TimeUnit.MINUTES.toMillis(15))
                                .withInactivityInterval(TimeUnit.MINUTES.toMillis(5))
                                .withMaxPartSize(1024 *2)
                                .build())
                .build();

        map.addSink(sink);


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
